package com.example.ssmlab1;

import org.apache.commons.dbcp.BasicDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DB {
    private static final BasicDataSource BASIC_DATA_SOURCE = new BasicDataSource();
    static {
        BASIC_DATA_SOURCE.setUrl("jdbc:mysql://?serverTimezone=UTC");
        BASIC_DATA_SOURCE.setUsername("root");
        BASIC_DATA_SOURCE.setPassword("123456");
        BASIC_DATA_SOURCE.setDriverClassName("com.mysql.cj.jdbc.Driver");
    }
    public static Connection getConnection(){
        try {
            return BASIC_DATA_SOURCE.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void closeSqlConnect(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(preparedStatement!=null){
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
